package com.ruoyi.video.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.video.Enum.AnalysisStatusEnum;
import com.ruoyi.video.service.IVideoLineItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.video.mapper.VideoAnalysisUserMapper;
import com.ruoyi.video.domain.VideoAnalysisUser;
import com.ruoyi.video.service.IVideoAnalysisUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 视频解析Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-06
 */
@Service
public class VideoAnalysisUserServiceImpl implements IVideoAnalysisUserService 
{
    @Autowired
    private VideoAnalysisUserMapper videoAnalysisUserMapper;

    @Autowired
    private IVideoLineItemService lineItemService;

    /**
     * 查询视频解析
     *
     * @param userId 视频解析主键
     * @param videoId 视频解析主键
     * @return 视频解析
     */
    @Override
    public VideoAnalysisUser selectVideoAnalysisUserById(Long userId, Long videoId)
    {
        return videoAnalysisUserMapper.selectVideoAnalysisUserById(userId, videoId);
    }

    /**
     * 查询视频解析列表
     * 
     * @param videoAnalysisUser 视频解析
     * @return 视频解析
     */
    @Override
    public List<VideoAnalysisUser> selectVideoAnalysisUserList(VideoAnalysisUser videoAnalysisUser)
    {
        return videoAnalysisUserMapper.selectVideoAnalysisUserList(videoAnalysisUser);
    }

    /**
     * 新增视频解析
     * 
     * @param videoAnalysisUser 视频解析
     * @return 结果
     */
    @Override
    public int insertVideoAnalysisUser(VideoAnalysisUser videoAnalysisUser)
    {
        return videoAnalysisUserMapper.insertVideoAnalysisUser(videoAnalysisUser);
    }

    /**
     * 修改视频解析
     * 
     * @param videoAnalysisUser 视频解析
     * @return 结果
     */
    @Override
    public int updateVideoAnalysisUser(VideoAnalysisUser videoAnalysisUser)
    {
        return videoAnalysisUserMapper.updateVideoAnalysisUser(videoAnalysisUser);
    }

    @Override
    public int batchUpdateVideoAnalysisUser(Long userId, List<Long> videoIds) {
        if (userId == null || videoIds == null || videoIds.isEmpty()) {
            return 0;
        }

        return videoAnalysisUserMapper.batchInsertOrUpdate(userId, videoIds, AnalysisStatusEnum.START.toString());
    }

    /**
     * 批量删除视频解析
     * 
     * @param ids 需要删除的视频解析主键
     * @return 结果
     */
    @Override
    public int deleteVideoAnalysisUserByIds(Long[] ids)
    {
        return videoAnalysisUserMapper.deleteVideoAnalysisUserByIds(ids);
    }

    /**
     * 删除视频解析信息
     * 
     * @param id 视频解析主键
     * @return 结果
     */
    @Override

    public int deleteVideoAnalysisUserById(Long id)
    {
        return videoAnalysisUserMapper.deleteVideoAnalysisUserById(id);
    }

    @Override
    @Transactional
    public int deleteVideoAnalysisUserById(Long userId, Long videoId) {
        VideoAnalysisUser user = videoAnalysisUserMapper.selectVideoAnalysisUserById(userId, videoId);
        if (StringUtils.isNull(user)) {
            return 0;
        }
        lineItemService.deleteVideoLineItemByUser(userId, videoId);
        deleteVideoAnalysisUserById(user.getId());
        return 1;
    }
}
